package com.play.ballen.home.ui

import android.content.Context
import android.content.Intent
import android.graphics.Typeface
import android.view.View
import com.play.ballen.R
import com.play.ballen.databinding.ActivityMessageBinding
import com.play.ballen.home.ui.fragment.MeBottleFragment
import com.play.ballen.home.vm.PLPFragmentVM
import com.play.ballen.im.fragment.PvtConversationFragment
import com.yuven.baselib.component.BaseMVVMActivity

class MessageActivity : BaseMVVMActivity<PLPFragmentVM, ActivityMessageBinding>() {

    private var showPosition = 0

    private val fragment1 by lazy { PvtConversationFragment.newInstance() }
    private val fragment2 by lazy { MeBottleFragment.newInstance() }


    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, MessageActivity::class.java)
            context.startActivity(starter)
        }
    }

    override fun layoutId(): Int {
        return R.layout.activity_message
    }

    override fun initObserver() {
        super.initObserver()
        viewModel.canGetNum.observe(this) {
            dataBinding.redCount.text = it.my_bottle.toString()
        }
    }
    override fun viewModelClass(): Class<PLPFragmentVM> {
        return PLPFragmentVM::class.java
    }

    override fun initView() {
    }

    override fun initData() {
        onChangeStyle(0)
        dataBinding.ivBtnBack.setOnClickListener{
            finish()
        }
        dataBinding.tvTitle1.setOnClickListener{
            onChangeStyle(0)
        }
        dataBinding.tvTitle2.setOnClickListener{
            onChangeStyle(1)
        }


    }

    override fun onResume() {
        super.onResume()
        viewModel.getCanGetNum()
    }

    override fun initIntent(intent: Intent?) {

    }

    private fun onChangeStyle(position: Int) {
        when (position) {
            0 -> {
                showPosition = 0
               dataBinding.tvTitle1.setTextColor(resources.getColor(R.color.green2))
                dataBinding.tvTitle2.setTextColor(resources.getColor(R.color.black))
                dataBinding.tvTitle1.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD))
                dataBinding.tvTitle2.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL))
                supportFragmentManager.beginTransaction()
                    .replace(R.id.fragment_content, fragment1).commitAllowingStateLoss()
            }
            1 -> {
                showPosition = 1
                dataBinding.tvTitle1.setTextColor(resources.getColor(R.color.black))
                dataBinding.tvTitle2.setTextColor(resources.getColor(R.color.green2))
                dataBinding.tvTitle1.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL))
                dataBinding.tvTitle2.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD))
                supportFragmentManager.beginTransaction()
                    .replace(R.id.fragment_content, fragment2)
                    .commitAllowingStateLoss()
            }
        }
    }
}